package com.gexiaobao.pigeon.ui.auction

import android.content.res.ColorStateList
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModelProvider
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.model.bean.Auction
import com.gexiaobao.pigeon.app.model.bean.TodayEnd
import com.gexiaobao.pigeon.app.model.bean.TomorrowEnd
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivityAuctionDetaillBinding
import com.gexiaobao.pigeon.ui.auction.fragment.FragmentFullAuction
import com.gexiaobao.pigeon.ui.auction.fragment.FragmentTodayCloseAuction
import com.gexiaobao.pigeon.ui.auction.fragment.FragmentTomorrowCloseAuction
import com.gexiaobao.pigeon.viewmodel.AuctionViewModel
import com.gexiaobao.pigeon.viewmodel.SharedViewModel
import com.google.android.material.tabs.TabLayout
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2024/11/12 11:23
 * @Describe    : 拍卖首页的详情
 */
class ActivityAuctionDetail : BaseActivity<AuctionViewModel, ActivityAuctionDetaillBinding>() {

    private var auctions: ArrayList<Auction> = arrayListOf()
    private var todayEnds: ArrayList<TodayEnd> = arrayListOf()
    private var tomorrowEnds: ArrayList<TomorrowEnd> = arrayListOf()
    private var mIndex = 0
    private var sharedViewModel: SharedViewModel? = null

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.toolBarLayout.toolBarLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.color_99CCFF))
        sharedViewModel = ViewModelProvider(this)[SharedViewModel::class.java]
        StatusBarUtil.setTranslucentForImageView(this, 0, null)

        mDatabind.toolBarLayout.tvTitle.text = "拍卖"
        mIndex = intent.getIntExtra("index", 0)
        initTabLayout()

        mDatabind.swipeRefreshLayout.init {
            mViewModel.getAuctionHome()
        }
        showLoading(getString(R.string.loading))
        mViewModel.getAuctionHome()
    }

    override fun createObserver() {
        super.createObserver()
        /** 获取app拍卖商品详情 */
        mViewModel.auctionHomeResult.observe(this) {
            if (mDatabind.swipeRefreshLayout.isRefreshing) {
                mDatabind.swipeRefreshLayout.isRefreshing = false
            }
            auctions.clear()
            auctions.addAll(it.auctions)
            todayEnds.clear()
            todayEnds.addAll(it.todayEnds)
            tomorrowEnds.clear()
            tomorrowEnds.addAll(it.tomorrowEnds)
            sharedViewModel?.setAuctionsData(auctions)
            sharedViewModel?.setTodayData(todayEnds)
            sharedViewModel?.setTomorrowData(tomorrowEnds)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.ivBackAuctionDetail, mDatabind.includeSearch.tvSearch, mDatabind.toolBarLayout.ivBack) {
            when (it) {
                mDatabind.toolBarLayout.ivBack -> finish()
                mDatabind.ivBackAuctionDetail -> finish()
                mDatabind.includeSearch.tvSearch -> {}
            }
        }
    }

    private fun initTabLayout() {
        mDatabind.tabLayout.tabRippleColor = ColorStateList.valueOf(ContextCompat.getColor(this, android.R.color.transparent))
        val tab1 = mDatabind.tabLayout.newTab().setText(getString(R.string.Closing_auction_today))
        val tab2 = mDatabind.tabLayout.newTab().setText(getString(R.string.tomorrow_end))
        val tab3 = mDatabind.tabLayout.newTab().setText(getString(R.string.auction_subject))
        mDatabind.tabLayout.addTab(tab1)
        mDatabind.tabLayout.addTab(tab2)
        mDatabind.tabLayout.addTab(tab3)

        mDatabind.tabLayout.addOnTabSelectedListener(object : TabLayout.BaseOnTabSelectedListener<TabLayout.Tab> {
            override fun onTabReselected(p0: TabLayout.Tab) {}
            override fun onTabUnselected(p0: TabLayout.Tab) {}

            override fun onTabSelected(p0: TabLayout.Tab) {
                replaceFragments(p0.position)
            }
        })
        replaceFragments(mIndex)
        mDatabind.tabLayout.getTabAt(mIndex)!!.select()
    }

    private fun replaceFragments(position: Int) {
        supportFragmentManager.beginTransaction().apply {
            when (position) {
                0 -> replace(R.id.frameLayout, FragmentTodayCloseAuction.newInstance())
                1 -> replace(R.id.frameLayout, FragmentTomorrowCloseAuction.newInstance())
                2 -> replace(R.id.frameLayout, FragmentFullAuction.newInstance())
            }
        }.commitAllowingStateLoss()
    }

}